package com.eastfair.imaster.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.base.entity.Entity;
import com.eastfair.core.base.entity.SuperEntity;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.code.ExceptionCode;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.imaster.dao.ShopWindowPointMapper;
import com.eastfair.imaster.entity.ExhibitorProduct;
import com.eastfair.imaster.entity.ShopWindowPoint;
import com.eastfair.imaster.service.ExhibitorProductService;
import com.eastfair.imaster.service.ShopWindowPointService;
import com.eastfair.imaster.vo.ExhibitorProductVO;
import com.eastfair.imaster.vo.ShopWindowPointVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 电子橱窗点位表
 * </p>
 *
 * @author zjn
 * @date 2022-03-15
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class ShopWindowPointServiceImpl extends SuperServiceImpl<ShopWindowPointMapper, ShopWindowPoint> implements ShopWindowPointService {

    @Autowired
    private ExhibitorProductService exhibitorProductService;

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ShopWindowPoint> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    @Override
    public R<Boolean> deleteByShopWindowId(Long id) {
        if (Objects.isNull(id)) {
            return R.fail(ExceptionCode.ERROR_NULL_ID);
        }
        UpdateWrapper<ShopWindowPoint> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(ShopWindowPoint::getShopWindowId, id).set(ShopWindowPoint::getIsDeleted, BusinessConstant.DELETE_YES);
        return R.success(update(updateWrapper));
    }

    @Override
    public R<List<ShopWindowPointVO>> getByShopWindowId(Long id) {
        if (Objects.isNull(id)) {
            return R.fail(ExceptionCode.ERROR_NULL_ID);
        }

        QueryWrapper<ShopWindowPoint> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ShopWindowPoint::getProjectId, ContextUtil.getProjectId())
                .eq(ShopWindowPoint::getExhibitionId, ContextUtil.getExhibitionId())
                .eq(ShopWindowPoint::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(ShopWindowPoint::getIsEnabled, BusinessConstant.ENABLE_YES)
                .eq(ShopWindowPoint::getShopWindowId, id)
                .orderByAsc(ShopWindowPoint::getPointIndex)
                .orderByDesc(Entity::getUpdateTime);
        List<ShopWindowPoint> shopWindowPointList = list(queryWrapper);
        if (CollectionUtil.isEmpty(shopWindowPointList)) {
            return R.success(Collections.EMPTY_LIST);
        }
        List<ShopWindowPointVO> result = ConvertUtil.convertList(shopWindowPointList, ShopWindowPointVO.class);

        return R.success(assembleExhibitorProduct(result));
    }

    /**
     * 组装点位展品信息
     *
     * @param shopWindowPointVOList 点位列表
     * @return 点位列表
     */
    private List<ShopWindowPointVO> assembleExhibitorProduct(List<ShopWindowPointVO> shopWindowPointVOList) {
        if (CollectionUtil.isNotEmpty(shopWindowPointVOList)) {
            List<Long> exhibitorProductIds = shopWindowPointVOList.stream().map(ShopWindowPointVO::getExhibitorProductId).collect(Collectors.toList())
                    .stream().filter(exhibitorProductId -> Objects.nonNull(exhibitorProductId)).collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(exhibitorProductIds)) {
                List<ExhibitorProduct> exhibitorProductList = exhibitorProductService.listByIds(exhibitorProductIds);
                if (CollectionUtil.isNotEmpty(exhibitorProductList)) {
                    Map<Long, ExhibitorProduct> collect = exhibitorProductList.stream().collect(Collectors.toMap(SuperEntity::getId, exhibitorProduct -> exhibitorProduct));
                    shopWindowPointVOList.parallelStream().forEach(shopWindowPointVO -> {
                        if (Objects.nonNull(shopWindowPointVO.getExhibitorProductId())) {
                            shopWindowPointVO.setExhibitorProductVO(BeanUtil.toBean(collect.get(shopWindowPointVO.getExhibitorProductId()), ExhibitorProductVO.class));
                        }
                    });
                }
            }
        }
        return shopWindowPointVOList;
    }
}
